<?php

/**
 * Description of o2orent
 *
 * @author libaoling
 * @time 2014-12-19
 */
class O2orentApp extends BackendApp
{
    private $_o2orent_mod;
    
    public function __construct()
    {
        $this->O2orentApp();
    }
    
    public function O2orentApp()
    {
        parent::BackendApp();
        $this->_o2orent_mod = m("o2orent");
    }
    
    public function index()
    {
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'floor',
                'type' => 'int',
            ),
            array(
                'field' => 'rent_no',
                'equal' => 'like',
            ),
        ));
        
        $floor_file = ROOT_PATH . '/includes/floor.inc.php';
        $floors = file_exists($floor_file) ? require $floor_file : array();

        $page = $this->_get_page(10);
        $rents = $this->_o2orent_mod->find(array(
            'conditions' => '1 = 1 ' . $conditions,
            'order' => 'rent_id DESC',
            'count' => true,
            'limit' => $page['limit'],
        ));
        
        $page['item_count'] = $this->_o2orent_mod->getCount();
        $this->_format_page($page);
        $this->assign('page_info', $page);
        $this->assign("rents", $rents);
        $this->assign("floors", $floors['rent']);
        $this->import_resource(array('script' => 'inline_edit.js'));

        $this->display("o2orent.index.html");
    }
    
    public function add()
    {
        if (!IS_POST)
        {
            /* 参数 */
            $floor_file = ROOT_PATH . '/includes/floor.inc.php';
            $floors = file_exists($floor_file) ? require $floor_file : array();
            
            $this->assign('floors', $floors['rent']);
            $this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
            
            $this->display('o2orent.form.html');
        }
        else
        {
            $data = $this->_get_save_data();
            if (!$rent_id = $this->_o2orent_mod->add($data))  //获取store_id
            {
                $this->show_warning($this->_o2orent_mod->get_error());
                return;
            }
            $image_url = $this->_upload_logo($rent_id);
            $image_url && $this->_o2orent_mod->edit($rent_id, array('image_url' => $image_url));

            $this->admin_log('添加出租位:' . $rent_id);
            $this->show_message('添加成功', 'back_list', 'index.php?app=o2orent', 'continue_add', 'index.php?app=o2orent&amp;act=add');
        }
    }
    
    public function edit()
    {
        $rent_id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        if (!IS_POST)
        {
            /* 参数 */
            $floor_file = ROOT_PATH . '/includes/floor.inc.php';
            $floors = file_exists($floor_file) ? require $floor_file : array();
            $rent = $this->_o2orent_mod->get_info($rent_id);
            
            $this->assign('rent', $rent);
            $this->assign('floors', $floors['rent']);
            $this->assign("act", "edit");
            $this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
            
            $this->display('o2orent.form.html');
        }
        else
        {
            $data = $this->_get_save_data();
            $image_url = $this->_upload_logo($rent_id);
            $image_url && $data['image_url'] = $image_url;
            $this->_o2orent_mod->edit($rent_id, $data);

            $this->admin_log('修改出租位:' . $rent_id);
            $this->show_message('修改成功', 'back_list', 'index.php?app=o2orent');
        }
    }
    
    public function drop()
    {
        $id = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$id)
        {
            $this->show_warning('没有此出租位');
            return;
        }

        $ids = explode(',', $id);
        $this->admin_log('删除出租位:' . $id);
        
        foreach ($ids as $id)
        {
            $this->_drop_storeo2o_image($id);
        }
        
        if (!$this->_o2orent_mod->drop($ids))
        {
            $this->show_warning($this->_o2orent_mod->get_error());
            return;
        }
        
        $this->show_message('删除成功');
    }
    
    private function _get_save_data()
    {
        $data = array();
        $data['rent_no'] = trim($_POST['rent_no']);
        $data['floor'] = trim($_POST['floor']);
        $data['area'] = trim($_POST['area']);
        $data['link_man'] = trim($_POST['link_man']);
        $data['tel'] = trim($_POST['tel']);
        $data['monthly'] = trim($_POST['monthly']);
        $data['monthly_unit'] = trim($_POST['monthly_unit']);
        $data['manage_fee'] = $_POST['manage_fee'];
        $data['manage_unit'] = $_POST['manage_unit'];
        $data['remark'] = trim($_POST['remark']);

        return $data;
    }
    
    public function check_rent_no()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $rent_no = empty($_GET['rent_no']) ? '' : trim($_GET['rent_no']);

        if (!$this->_o2orent_mod->unique_rent_no($rent_no, $id))
        {
            echo ecm_json_encode(false);
            return;
        }
        echo ecm_json_encode(true);
    }
    
    private function _upload_logo($rent_id)
    {
        $file = $_FILES["image_url"];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
        {
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES["image_url"]); //上传logo

        if (!$uploader->file_info())
        {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=o2orent&amp;act=edit&amp;id=' . $rent_id);
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);
        /* 上传 */
        if ($file_path = $uploader->save('data/files/o2o/rent', $rent_id))   //保存到指定目录，并以指定文件名$adv_id存储
        {
            return $file_path;
        }
        else
        {
            return false;
        }
    }
    
    private function _drop_storeo2o_image($rent_id)
    {
        $info = $this->_o2orent_mod->getCol("select image_url from {$this->_o2orent_mod->table} where rent_id = {$rent_id} ");
        $this->_drop_image($info[0]);
    }
    
    private function _drop_image($image_url)
    {
        $filename = ROOT_PATH . "/" . $image_url;
        if (file_exists($filename))
        {
            @unlink($filename);
        }
    }
    
    
}
